package com.xyz.service.impl;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;
import java.util.stream.Collectors;

import javax.transaction.Transactional;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.xyz.dto.CartDTO;
import com.xyz.dto.OrderDTO;
import com.xyz.enums.OrderStatusEnum;
import com.xyz.enums.PayStatusEnum;
import com.xyz.enums.ResultEnum;
import com.xyz.exception.SellException;
import com.xyz.pojo.OrderDetail;
import com.xyz.pojo.OrderMaster;
import com.xyz.pojo.ProductInfo;
import com.xyz.repository.OrderDetailRepository;
import com.xyz.repository.OrderMasterRepository;
import com.xyz.service.OrderService;
import com.xyz.service.ProductService;
import com.xyz.utils.KeyUtil;

@Service
public class OrderServiceImpl implements OrderService {

	@Autowired
	private ProductService productService;

	@Autowired
	private OrderDetailRepository orderDetailRepository;

	@Autowired
	private OrderMasterRepository orderMasterRepository;

	@Override
	@Transactional
	public OrderDTO create(OrderDTO orderDTO) {

		BigDecimal orderAmount = new BigDecimal(BigInteger.ZERO);
		String orderId = KeyUtil.genUniqueKey();

		// List<CartDTO> cartDTOList = new ArrayList<CartDTO>();

		// 1. 查询商品的数量，价格
		for (OrderDetail orderDetail : orderDTO.getOrderDetailList()) {
			ProductInfo productInfo = productService.findById(orderDetail.getProductId());
			if (null == productInfo) {
				throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
			}
			// 2. 计算订单总价
			orderAmount = productInfo.getProductPrice().multiply(new BigDecimal(orderDetail.getProductQuantity()))
					.add(orderAmount);

			// 订单详情入库
			orderDetail.setDetailId(KeyUtil.genUniqueKey());
			orderDetail.setOrderId(orderId);

			BeanUtils.copyProperties(productInfo, orderDetail);

			orderDetailRepository.save(orderDetail);

			// cartDTOList.add(new CartDTO(orderDetail.getProductId(),
			// orderDetail.getProductQuantity()));
		}

		// 写入订单数据库
		OrderMaster orderMaster = new OrderMaster();
		BeanUtils.copyProperties(orderDTO, orderMaster);
		orderMaster.setOrderId(orderId);
		orderMaster.setOrderAmount(orderAmount);
		orderMaster.setOrderStatus(OrderStatusEnum.NEW.getCode());
		orderMaster.setPayStatus(PayStatusEnum.WAIT.getCode());

		orderMasterRepository.save(orderMaster);
		// 下单成功扣库存
		List<CartDTO> cartDTOList = orderDTO.getOrderDetailList().stream()
				.map(e -> new CartDTO(e.getProductId(), e.getProductQuantity())).collect(Collectors.toList());

		productService.decreaseStock(cartDTOList);

		return orderDTO;
	}

	@Override
	public OrderDTO findById(String orderId) {

		OrderMaster orderMaster = orderMasterRepository.findById(orderId).get();
		if (null == orderMaster) {
			throw new SellException(ResultEnum.ORDER_NOT_EXIST);
		}

		List<OrderDetail> orderDetailList = orderDetailRepository.findByOrderId(orderId);
		if (CollectionUtils.isEmpty(orderDetailList)) {
			throw new SellException(ResultEnum.ORDERDETAIL_NOT_EXIST);
		}
		OrderDTO orderDTO = new OrderDTO();
		BeanUtils.copyProperties(orderMasterRepository, orderDTO);
		orderDTO.setOrderDetailList(orderDetailList);

		return orderDTO;
	}

	@Override
	public Page<OrderDTO> findList(String buyerOpenId, Pageable pageable) {
		
		
		return null;
	}

	@Override
	public OrderDTO cancel(OrderDTO orderDTO) {
		
		
		return null;
	}

	@Override
	public OrderDTO finish(OrderDTO orderDTO) {
		
		
		return null;
	}

	@Override
	public OrderDTO paid(OrderDTO orderDTO) {
		
		
		return null;
	}

}
